package by.semerikov.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import by.semerikov.converter.PersonConverter;
import by.semerikov.dao.DAOFactory;
import by.semerikov.dao.IPersonDAO;
import by.semerikov.data.Connector;
import by.semerikov.entity.PersonEntity;
import by.semerikov.exceptions.ConnectorException;
import by.semerikov.exceptions.DAOException;
import by.semerikov.model.Person;

public class PersonService {
	PersonConverter personConverter = new PersonConverter();
	
	
	public List<Person> getPersonsEqual(Person example) throws DAOException, ConnectorException
	{
		List<Person> result = new ArrayList<Person>();
		List<PersonEntity> resultEntity = new ArrayList<PersonEntity>();
		try {
			PersonEntity exampleEntity = personConverter.convertToEntity(example);
			Connection conn = Connector.getConnection();
			conn.setAutoCommit(false);
			IPersonDAO pd = DAOFactory.getInstance().getPersonDAO();
			pd.setConnection(conn);
			resultEntity = pd.read(exampleEntity);
			conn.setAutoCommit(true);
			conn.close();
			for (int i = 0; i < resultEntity.size(); i++) {
				result.add(personConverter.convertToModel(resultEntity.get(i)));
			}
		} catch (SQLException e) {
			throw new ConnectorException("exception.service.person.read",e);
		}
		return result;
		
	}
}
